/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.services.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureDAO;
import cz.muni.fi.pa165.creatures.dao.RegionDAO;
import cz.muni.fi.pa165.creatures.dto.RegionDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.CreatureMapping;
import cz.muni.fi.pa165.creatures.dto.mapping.RegionMapping;
import cz.muni.fi.pa165.creatures.model.Creature;
import cz.muni.fi.pa165.creatures.model.Region;
import cz.muni.fi.pa165.creatures.services.RegionService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents actual implementation of the
 * {@link cz.muni.fi.pa165.creatures.services.RegionService}.
 *
 * @author hulek
 */
@Service
public final class RegionServiceImpl implements RegionService {

    private static final Logger logger =
            Logger.getLogger(RegionServiceImpl.class.getName());

    @Autowired
    private RegionDAO regionDAO;

    @Autowired
    private RegionMapping regionMapping;

    @Autowired
    private CreatureDAO creatureDAO;

    @Autowired
    private CreatureMapping creatureMapping;

    @Override
    @Transactional
    public Long create(final RegionDTO region) {
        try {
            return regionDAO.create(regionMapping.DTOtoEntity(region));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void remove(final RegionDTO region) {
        try {
            regionDAO.remove(regionMapping.DTOtoEntity(region));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void update(final RegionDTO region) {
        try {
            regionDAO.update(regionMapping.DTOtoEntity(region));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public RegionDTO getById(final Long id) {
        try {
            Region region = regionDAO.get(id);
            RegionDTO regionDTO = regionMapping.entityToDTO(region);
            List<Creature> creatures = creatureDAO.getCreaturesOf(region);
            if (creatures != null) {
                regionDTO.setCreaturesCount(Long.valueOf(creatures.size()));
            }
            return regionDTO;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<RegionDTO> getAll() {
        try {
            List<RegionDTO> dtoCollection = new ArrayList<RegionDTO>();
            List<Creature> creatures;
            for (Region region : regionDAO.getAll()) {
                RegionDTO regionDTO = regionMapping.entityToDTO(region);
                creatures = creatureDAO.getCreaturesOf(region);
                if (creatures != null) {
                    regionDTO.setCreaturesCount(Long.valueOf(creatures.size()));
                }
                dtoCollection.add(regionDTO);
            }
            return dtoCollection;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Long getCount() {
        try {
            return regionDAO.getCount();
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    public void deleteAll() {
        try {
            regionDAO.deleteAll();
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    public RegionDTO getByName(final String name) {
        try {
            Region region = regionDAO.getByName(name);
            return regionMapping.entityToDTO(region);
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }
}